﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplicationFileSys.Models;

namespace WebApplicationFileSys.Controllers
{
    /// <summary>
    /// 登录，处理
    /// </summary>
    /// 添加时间：2023-8-31 17:44:08，
    public class LoginHandleController : Controller
    {
        //视图，登录
        public ActionResult LoginView()
        {
            return View();
        }

        //退出登录
        public ActionResult ExitLogin()
        {
            Session.Remove(KeyValueConfig.KeyUserSession);
            return Redirect("/home/index");
        }


        //处理登录，
        public ActionResult DoLogin(string account, string pwd)
        {
            if (string.IsNullOrWhiteSpace(account))
            {
                return Json(new Result("账号不可为空"));
            }
            if (string.IsNullOrWhiteSpace(pwd))
            {
                return Json(new Result("密码不可为空"));
            }

            //超级管理员，读写文件
            string adminAccount = System.Configuration.ConfigurationManager.AppSettings["user_admin"];
            string user_admin_password = System.Configuration.ConfigurationManager.AppSettings["user_admin_password"];
            if (account.Trim().Equals(adminAccount))
            {
                if (!pwd.Trim().Equals(user_admin_password))
                {
                    return Json(new Result("密码错误"));
                }
                Session[KeyValueConfig.KeyUserSession] = adminAccount;
                goto TO_NEXT;
            }


            string user_read = System.Configuration.ConfigurationManager.AppSettings["user_read"];
            string user_read_password = System.Configuration.ConfigurationManager.AppSettings["user_read_password"];
            //普通用户，只读文件
            if (account.Trim().Equals(user_read))
            {
                if (!pwd.Trim().Equals(user_read_password))
                {
                    return Json(new Result("密码错误"));
                }
                Session[KeyValueConfig.KeyUserSession] = user_read;
            }

        TO_NEXT:

            return Json(new Result("登录成功", 1));
        }


    }
}